package Screens.Manager;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

/** Screen manager class
 * @author Aikidos
 */
public class ScreenManager {

	/** List of screen */
	private static Map<String, Screen> _lstScreens = new HashMap<String, Screen>();
	
	/** Current active screen */
	private static Screen _activeScreen;
	
	/** Previous screen */
	private static Screen _previousScreen;

	/** Initialize screen manager */
	public static void initialize() {
		
	}
	
	/** Update active screen
	 * @param delta - Time delta
	 */
	public static void update(int delta) throws SlickException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
		if (_activeScreen != null)
			_activeScreen.update(delta);
	}
	
	/** Draw active screen
	 * @param g - Slick2d graphics
	 */
	public static void draw(Graphics g) throws SlickException {
		if (_activeScreen != null)
			_activeScreen.draw(g);
	}
	
	/** Add new screen 
	 * @param name - Screen name
	 * @param screen - Screen
	 */
	public static void addScreen(String name, Screen screen) {
		if (!_lstScreens.containsKey(name))
			_lstScreens.put(name, screen);
	}
	
	/** Returns screen
	 * @param name - Screen name
	 * @return Screen
	 */
	public static Screen get(String name) {
		return _lstScreens.containsKey(name) ? _lstScreens.get(name) : null;
	}
	
	/** Set screen as active
	 * @param name - Screen name
	 */
	public static void go(String name) throws SlickException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
		if (_lstScreens.containsKey(name))
		{
			if (_activeScreen != null)
			{
				_previousScreen = _activeScreen;
				_previousScreen.shutdown();
				_previousScreen.unloadContent();
			}
			
			_activeScreen = _lstScreens.get(name);
			_activeScreen.loadContent();
			_activeScreen.init();
		}
	}
	
	/** Delete screen
	 * @param name - Screen name
	 * @throws SlickException 
	 */
	public static void delScreen(String name) throws SlickException {
		if (_lstScreens.containsKey(name))
		{
			if (_activeScreen != _lstScreens.get(name))
				_lstScreens.remove(name);
			else
			{
				_lstScreens.get(name).shutdown();
				_lstScreens.get(name).unloadContent();
				_lstScreens.remove(name);
			}
		}
	}
}
